package com.qen.fx.custom;

import cn.hutool.core.util.StrUtil;
import com.qen.App;
import com.qen.fx.ViewIndex;
import com.qen.fx.custom.impl.pwd.AddPwd;
import com.qen.fx.custom.impl.DefaultBusiness;
import com.qen.fx.custom.impl.pwd.HashTxt;
import com.qen.fx.custom.impl.pwd.TokenCreate;
import com.qen.fx.custom.impl.pwd.UuidGenerator;
import com.qen.fx.factory.AllMenuCallback;
import com.qen.fx.factory.SideCallback;
import javafx.beans.property.*;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Data
public class CustomContent extends ScrollPane implements Initializable, SideCallback, AllMenuCallback, Serializable {

    private final StringProperty titleText;
    private final StringProperty contentText;
    private final BooleanProperty isLike;
    private final StringProperty action;
    private ConcurrentMap<String, CustomBusiness> businessMap = new ConcurrentHashMap<>();
    @FXML
    private ContentHeader contentHeader;
    @FXML
    private CustomBusiness cuctomBusiness;
    @FXML
    private VBox businessVBox;

    public void setCustomSide(CustomSide customSide) {
        customSide.setSideCallback(this);
        this.customSide = customSide;
    }

    private CustomSide customSide;

    @FXML
    private ScrollPane contentScrollpane;
    @FXML
    private AnchorPane contentAnchorPane;

    public CustomContent() {
        titleText = new SimpleStringProperty();
        contentText = new SimpleStringProperty();
        action = new SimpleStringProperty();
        isLike = new SimpleBooleanProperty(false);
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(ViewIndex.CustomContent));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        CustomContent controller = fxmlLoader.getController();
        controller.initCustomContent();

        contentScrollpane.prefHeight(ViewIndex.sidePreferHeight);
        contentScrollpane.prefWidth(ViewIndex.contentPreferWidth);
        contentScrollpane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

        contentAnchorPane.setPrefHeight(ViewIndex.sidePreferHeight);
        contentAnchorPane.setPrefWidth(ViewIndex.contentPreferWidth);

        contentHeader.setVisible(false);
        contentHeader.setManaged(false);
        DefaultBusiness customBusiness = new DefaultBusiness();
        customBusiness.setAllMenuCallback(this);
        businessVBox.getChildren().addLast(customBusiness);
    }

    public void initCustomContent() {

    }

    public CustomContent(Node... nodes) {
        this();
        VBox[] vBox = new VBox[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            vBox[i] = new VBox(nodes[i]);
        }
        AnchorPane pane = new AnchorPane(vBox);
        pane.setPrefHeight(ViewIndex.sidePreferHeight);
        pane.setPrefWidth(-1);
        this.setContent(pane);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
//        CustomSide customSide = getCustomSide(); // Implement this method to get the CustomSideController instance
//        customSide.setSideCallback(this);
    }

    public String getTitleText() {
        return titleText.get();
    }

    public StringProperty titleTextProperty() {
        return titleText;
    }

    public void setTitleText(String titleText) {
        this.titleText.set(titleText);
    }

    public String getContentText() {
        return contentText.get();
    }

    public StringProperty contentTextProperty() {
        return contentText;
    }

    public void setContentText(String contentText) {
        this.contentText.set(contentText);
    }

    public boolean isIsLike() {
        return isLike.get();
    }

    public BooleanProperty isLikeProperty() {
        return isLike;
    }

    public void setIsLike(boolean isLike) {
        this.isLike.set(isLike);
    }

    public String getAction() {
        return action.get();
    }

    public StringProperty actionProperty() {
        return action;
    }

    public void setAction(String action) {
        this.action.set(action);
    }

    @Override
    public void seleteMenu(String title, String content, Boolean isLike, String action) {
        this.titleText.set(title);
        this.contentText.set(content);
        this.isLike.set(isLike);
        this.action.set(action);
        contentHeader.setContentText(content);
        contentHeader.setTitleText(title);
        contentHeader.setIsLike(isLike);
        contentHeader.updateText();
        int len = businessVBox.getChildren().size();
        if (len > 1) {
            List<Node> list = businessVBox.getChildren();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) instanceof CustomBusiness) {
                    CustomBusiness cb = (CustomBusiness) list.get(i);
                    cb.setManaged(false);
                    cb.setVisible(false);
                }
            }
        }
        CustomBusiness cbs = null;
        if (StrUtil.isNotBlank(action)) {
            cbs = businessMap.get(action);
        }
        if (cbs != null) {
            contentHeader.setVisible(true);
            contentHeader.setManaged(true);
            cbs.setVisible(true);
            cbs.setManaged(true);
        } else {
            contentHeader.setVisible(true);
            contentHeader.setManaged(true);
            switch (action){
                case "tokenCreate":cbs = new TokenCreate();break;
                case "hashTxt":cbs = new HashTxt();break;
                case "addPwd":cbs = new AddPwd();break;
                case "uuidGenerator":cbs = new UuidGenerator();break;
                case "ulidGenerator":break;
                case "encryption":break;
                case "bip39Generator":break;
                case "hmacGenerator":break;
                case "rsaKeyPairGenerator":break;
                case "passwordStrengthAnalyser":break;
                case "pdfSignatureChecker":break;

                case "dateConverter":break;
                case "baseConverter":break;
                case "romanNumeralConverter":break;
                case "base64StringConverter":break;
                case "base64FileConverter":break;
                case "colorConverter":break;
                case "caseConverter":break;
                case "textToNatoAlphabet":break;
                case "textToBinary":break;
                case "textToUnicode":break;
                case "yamlToJsonConverter":break;
                case "yamlToToml":break;
                case "jsonToYamlConverter":break;
                case "jsonToToml":break;
                case "listConverter":break;
                case "tomlToJson":break;
                case "tomlToYaml":break;

                case "urlEncoder":break;
                case "htmlEntities":break;
                case "urlParser":break;
                case "deviceInformation":break;
                case "basicAuthGenerator":break;
                case "ogMetaGenerator":break;
                case "otpGenerator":break;
                case "mimeTypes":break;
                case "jwtParser":break;
                case "keycodeInfo":break;
                case "slugifyString":break;
                case "htmlWysiwygEditor":break;
                case "userAgentParser":break;
                case "httpStatusCodes":break;
                case "jsonDiff":break;
                case "safelinkDecoder":break;

                case "qrcodeGenerator":break;
                case "wifiQrcodeGenerator":break;
                case "svgPlaceholderGenerator":break;
                case "cameraRecorder":break;

                case "gitMemo":break;
                case "randomPortGenerator":break;
                case "crontabGenerator":break;
                case "jsonPrettify":break;
                case "jsonMinify":break;
                case "jsonToCsv":break;
                case "sqlPrettify":break;
                case "chmodCalculator":break;
                case "dockerRunToDockerComposeConverter":break;
                case "xmlFormatter":break;
                case "yamlPrettify":break;

                case "ipv4SubnetCalculator":break;
                case "ipv4AddressConverter":break;
                case "ipv4RangeExpander":break;
                case "macAddressLookup":break;
                case "macAddressGenerator":break;
                case "ipv6UlaGenerator":break;

                case "mathEvaluator":break;
                case "etaCalculator":break;
                case "percentageCalculator":break;

                case "chronometer":break;
                case "temperatureConverter":break;
                case "benchmarkBuilder":break;

                case "loremIpsumGenerator":break;
                case "textStatistics":break;
                case "emojiPicker":break;
                case "stringObfuscator":break;
                case "textDiff":break;
                case "numeronymGenerator":break;
                case "asciiTextDrawer":break;

                case "phoneParserAndFormatter":break;
                case "ibanValidatorAndParser":break;
                default:
                    contentHeader.setVisible(false);
                    contentHeader.setManaged(false);
                    cbs = new DefaultBusiness();
                    ((DefaultBusiness) cbs).setAllMenuCallback(this);
                    break;
            }
//            if ("tokenCreate".equals(action)) {
//                cbs = new TokenCreate();
//            } else if ("hashTxt".equals(action)) {
//                cbs = new HashTxt();
//            } else if ("addPwd".equals(action)) {
//                cbs = new AddPwd();
//            } else {
//                contentHeader.setVisible(false);
//                contentHeader.setManaged(false);
//                cbs = new DefaultBusiness();
//                ((DefaultBusiness) cbs).setAllMenuCallback(this);
//            }
            if (!(cbs instanceof DefaultBusiness)) {
                businessMap.put(action, cbs);
            }
            businessVBox.getChildren().addLast(cbs);
        }
    }

    @Override
    public void seleteBusiness(String title, String content, Boolean isLike, String action) {
        seleteMenu(title, content, isLike, action);
    }

}
